package samber

import (
	"fmt"
	"github.com/samber/lo"
	"math"
)

// Sample 随机返回一个
func Sample() {
	res := lo.Sample([]int64{1, 2, 3})
	fmt.Printf("%v \n", res)
}

// Samples 随机返回N个 每次触发的顺序一样
func Samples() {
	res := lo.Samples([]int64{1, 2, 3}, 2)
	fmt.Printf("%v \n", res) // [1 2]
	res = lo.Samples([]int64{1, 2, 3}, 4)
	fmt.Printf("%v \n", res) // [3 2 1]
}

// Shuffle 洗数组 [2 1 3 4 5 0]
func Shuffle() {
	list := []int{0, 1, 2, 3, 4, 5}
	result := lo.Shuffle(list)
	fmt.Printf("%v \n", result)
}

// Slice [S:E]写法
func Slice() {
	list := []int{0, 1, 2, 3, 4, 5}
	result := lo.Slice(list, 1, 4)
	fmt.Printf("%v\n", result) // [1 2 3]
}

// SliceToChannel 数组转仅可读的管道
func SliceToChannel() {
	list := []int{0, 1, 2, 3}
	channel := lo.SliceToChannel(5, list)
	for v := range channel {
		fmt.Printf("%v\n", v) // 0 1 2 3
	}
}

// SliceToMap 数组按照自己的逻辑转Map
func SliceToMap() {
	list := []string{"a", "aa", "aaa"}
	result := lo.Associate(list, func(str string) (string, int) {
		return str, len(str)
	})
	fmt.Printf("%v\n", result) // map[a:1 aa:2 aaa:3]
}

// Some 子集至少有一个
func Some() {
	list1 := []int{0, 1, 2, 3, 4}
	list2 := []int{1, 5}
	result := lo.Some(list1, list2)
	fmt.Printf("%v\n", result) // true
}

// SomeBy 有一个满足逻辑就行
func SomeBy() {
	list1 := []int{0, 1, 2, 3, 4}
	result := lo.SomeBy(list1, func(item int) bool {
		return item > 3
	})
	fmt.Printf("%v\n", result) // true
}

// Subset 数组偏移量加长度
func Subset() {
	list := []int{0, 1, 2, 3, 4, 5, 0, 1, 2, 3}
	result := lo.Subset(list, 2, 3)
	fmt.Printf("%v\n", result) // [2 3 4]
}

// Substring 字符串偏移量加长度
func Substring() {
	result1 := lo.Substring("hello", 2, 3)
	result2 := lo.Substring("hello", -4, 3)
	result3 := lo.Substring("hello", -2, math.MaxUint)

	fmt.Printf("%v\n", result1) // llo
	fmt.Printf("%v\n", result2) // ell
	fmt.Printf("%v\n", result3) // lo
}

// Sum 求和
func Sum() {
	list := []int{1, 2, 3, 4, 5}
	result := lo.Sum(list)
	fmt.Printf("%v\n", result) // 15
}

// SumBy 按照逻辑求和
func SumBy() {
	list := []string{"foo", "bar"}
	result := lo.SumBy(list, func(item string) int {
		return len(item)
	})
	fmt.Printf("%v\n", result) // 6
}

func Switch() {
	result := lo.Switch[int, string](1).
		Case(1, "1").
		Case(2, "2").
		Default("3")
	fmt.Printf("%v\n", result) // 1
}
